package action.agent;

import org.apache.struts2.ServletActionContext;

import security.MySecurityContextHandle;
import service.interf.IIfwAgentRegInfo;
import action.common.MyBaseAction;

import com.opensymphony.xwork2.ModelDriven;

import dao.interf.IIfwAgentRegInfoDAO;
import dao.pojo.IfwAgentRegInfo;

public class IfwAgentRegInfoAction extends MyBaseAction<IfwAgentRegInfo>
		implements ModelDriven<IfwAgentRegInfo> {

	private static final long serialVersionUID = 1L;

	public IfwAgentRegInfoAction() {
		super(IfwAgentRegInfo.class);
	}

	public IfwAgentRegInfo getModel() {
		return model;
	}

	private IIfwAgentRegInfo iIfwAgentRegInfo;
	private IIfwAgentRegInfoDAO ifwAgentRegInfoDAO;

	public IIfwAgentRegInfo getiIfwAgentRegInfo() {
		return iIfwAgentRegInfo;
	}

	public void setiIfwAgentRegInfo(IIfwAgentRegInfo iIfwAgentRegInfo) {
		this.iIfwAgentRegInfo = iIfwAgentRegInfo;
	}

	public IIfwAgentRegInfoDAO getIfwAgentRegInfoDAO() {
		return ifwAgentRegInfoDAO;
	}

	public void setIfwAgentRegInfoDAO(IIfwAgentRegInfoDAO ifwAgentRegInfoDAO) {
		this.ifwAgentRegInfoDAO = ifwAgentRegInfoDAO;
	}

	private String password;
	private String oldPwd;

	public void setPassword(String password) {
		this.password = password;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	/**
	 * 初始化列表
	 */
	public void getlist() {
		String x = "";
		try {
			x = iIfwAgentRegInfo.findByProperty("IfwAgentRegInfo", "*", "json",
					true, null, null, null, null, model.getPage(), model
							.getRows());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}

	/**
	 * 查询--name
	 */
	public void findByName() {
		String x = "";
		try {
			x = iIfwAgentRegInfo.findByProperty("IfwAgentRegInfo", "*", "json",
					true, new String[] { "agentName" }, new Object[] { model
							.getAgentName() }, null, null, model.getPage(),
					model.getRows());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}

	/**
	 * 注册代理商
	 */
	public void addAgent() {
		String x = "";
		try {
			x = iIfwAgentRegInfo.addAgent(model, password);
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

	/**
	 * 消费点卡
	 */
	public void consumpPoint() {
		String x = "";
		try {
			x = iIfwAgentRegInfo.consumpPoint(model.getAgentIndex(), model
					.getAgentPoint());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

	/**
	 * 查询--ID
	 */
	public void findById() {
		String x = "";
		try {
			if (model.getAgentIndex() != null) {
				x = iIfwAgentRegInfo.findByProperty("IfwAgentRegInfo", "*",
						"json", true, new String[] { "agentIndex" },
						new Object[] { model.getAgentIndex() }, null, null,
						model.getPage(), model.getRows());
			} else {
				Integer agentIndex = MySecurityContextHandle.getAgentIndex();
				x = iIfwAgentRegInfo.findByProperty("IfwAgentRegInfo", "*",
						"json", true, new String[] { "agentIndex" },
						new Object[] { agentIndex }, null, null, model
								.getPage(), model.getRows());
			}
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

	/**
	 * 修改基本信息
	 */
	public void changeAgent() {
		String x = "";
		try {
			x = iIfwAgentRegInfo.changeAgent(model);
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

	/**
	 * 修改帐号密码
	 */
	public void changePwd() {
		String x = "";
		try {
			x = iIfwAgentRegInfo.changePwd(model, oldPwd, password);
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}
}
